home *** CD-ROM | disk | FTP | other *** search
/ 220 Jogos / 220 jogos.iso / classicos / genius3 / TEXTURAS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-21  |  4.0 KB  |  100 lines

  1.  
  2. #include <gl\glaux.h>
  3. #include <stdio.h>    
  4. #include "texturas.h"    
  5.  
  6. bool CargaTGA(TexturaTGA *textura, char *filename)    
  7. {    
  8.  
  9.     GLubyte        TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};        // Uncompressed TGA Header
  10.     GLubyte        TGAcompare[12];                                    // Used To Compare TGA Header
  11.     GLubyte        header[6];                                        // First 6 Useful Bytes From The Header
  12.     GLuint        bytesPerPixel;                                    // Holds Number Of Bytes Per Pixel Used In The TGA File
  13.     GLuint        imageSize;                                        // Used To Store The Image Size When Setting Aside Ram
  14.     GLuint        temp;                                            // Temporary Variable
  15.     GLuint        type=GL_RGBA;                                    // Set The Default GL Mode To RBGA (32 BPP)
  16.     
  17.     FILE *file = fopen(filename, "rb");                            // Open The TGA File
  18.  
  19.     if(    file==NULL ||                                            // Does File Even Exist?
  20.         fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||    // Are There 12 Bytes To Read?
  21.         memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0                ||    // Does The Header Match What We Want?
  22.         fread(header,1,sizeof(header),file)!=sizeof(header))                // If So Read Next 6 Header Bytes
  23.     {
  24.         if (file == NULL)                                        // Did The File Even Exist? *Added Jim Strong*
  25.             return FALSE;                                        // Return False
  26.         else                                                    // Otherwise
  27.         {
  28.             fclose(file);                                        // If Anything Failed, Close The File
  29.             return FALSE;                                        // Return False
  30.         }
  31.     }
  32.  
  33.     textura->ancho  = header[1] * 256 + header[0];                // Determine The TGA ancho    (highbyte*256+lowbyte)
  34.     textura->alto = header[3] * 256 + header[2];                // Determine The TGA alto    (highbyte*256+lowbyte)
  35.     
  36.      if(    textura->ancho    <=0    ||                                    // Is The ancho Less Than Or Equal To Zero
  37.         textura->alto    <=0    ||                                    // Is The alto Less Than Or Equal To Zero
  38.         (header[4]!=24 && header[4]!=32))                        // Is The TGA 24 or 32 Bit?
  39.     {
  40.         fclose(file);                                            // If Anything Failed, Close The File
  41.         return FALSE;                                            // Return False
  42.     }
  43.  
  44.     textura->bpp    = header[4];                                // Grab The TGA's Bits Per Pixel (24 or 32)
  45.     bytesPerPixel    = textura->bpp/8;                            // Divide By 8 To Get The Bytes Per Pixel
  46.     imageSize        = textura->ancho*textura->alto*bytesPerPixel;    // Calculate The Memory Required For The TGA Data
  47.  
  48.     textura->imageData=(GLubyte *)malloc(imageSize);            // Reserve Memory To Hold The TGA Data
  49.  
  50.     if(    textura->imageData==NULL ||                                // Does The Storage Memory Exist?
  51.         fread(textura->imageData, 1, imageSize, file)!=imageSize)    // Does The Image Size Match The Memory Reserved?
  52.     {
  53.         if(textura->imageData!=NULL)                            // Was Image Data Loaded
  54.             free(textura->imageData);                            // If So, Release The Image Data
  55.  
  56.         fclose(file);                                            // Close The File
  57.         return FALSE;                                            // Return False
  58.     }
  59.     for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)            // Loop Through The Image Data
  60.     {                                                            // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
  61.         temp=textura->imageData[i];                                // Temporarily Store The Value At Image Data 'i'
  62.         textura->imageData[i] = textura->imageData[i + 2];        // Set The 1st Byte To The Value Of The 3rd Byte
  63.         textura->imageData[i + 2] = temp;                        // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
  64.     }
  65.  
  66.     fclose (file);                                                // Close The File
  67.  
  68.     // Build A textura From The Data
  69.     
  70.     glGenTextures(1, &textura->texID);                        // Generate OpenGL textura IDs
  71.  
  72.     glBindTexture(GL_TEXTURE_2D, textura->texID);                // Bind Our Texture
  73.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);    // Linear Filtered
  74.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);    // Linear Filtered
  75.     
  76.     if (textura->bpp==24)                                        // Was The TGA 24 Bits
  77.     {
  78.         type=GL_RGB;                                            // If So Set The 'type' To GL_RGB
  79.     }
  80.  
  81.     glTexImage2D(GL_TEXTURE_2D, 0, type, textura->ancho, textura->alto, 0, type, GL_UNSIGNED_BYTE, textura->imageData);
  82.  
  83.     return TRUE;                                                // Texture Building Went Ok, Return True
  84. }
  85.  
  86. bool Esta(char *nomtext,AUX *auxText,int numtext,int t)
  87. {
  88.     t=0;
  89.     bool esta=FALSE;
  90.  
  91.     while ((t<numtext)&&(!esta))
  92.     {
  93.         if (!strcmp(nomtext,auxText[t].nombre))
  94.         {
  95.             esta=TRUE;
  96.         }
  97.         t++;
  98.     }
  99.     return esta;
  100. }